package com.edu.cuotibook.aop;

import com.edu.cuotibook.entity.common.CommonException;
import com.edu.cuotibook.entity.common.ResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * create by sym  2021/3/24 20:55.
 * 全局异常处理
 */
@Slf4j
@ControllerAdvice
public class GloableExecptionHandler {

    @ExceptionHandler(value = CommonException.class)
    public @ResponseBody
    ResultDTO handCommonEx(CommonException c, HttpServletRequest httpServletRequest){
        log.error("GloableExecptionHandler CommonException  url {} msg {}",httpServletRequest.getRequestURI(),c.getMsg(),c);
        return ResultDTO.fail(c);
    }

    @ExceptionHandler(value = Throwable.class)
    public @ResponseBody
    ResultDTO handThroawble(Throwable t, HttpServletRequest httpServletRequest){
        log.error("GloableExecptionHandler  Throwable url {} msg {}",httpServletRequest.getRequestURI(),t.getMessage(),t);
        return ResultDTO.fail();
    }
}
